// 1. 创建一个接口 ProductInterface
interface ProductInterface {
  id: number
  name: string
  price: number
  category: Category
  getDetails(): string
}

// 2. 创建一个枚举类型 Category
enum Category {
  Electronics,
  Clothing,
  Food
}

// 3. 创建一个类 Product
class Product implements ProductInterface {
  id: number
  name: string
  price: number
  category: Category
  private _stock: number
  private static taxRate: number = 0.08 // 默认税率为8%

  constructor(id: number, name: string, price: number, category: Category, stock: number) {
    this.id = id
    this.name = name
    this.price = price
    this.category = category
    this._stock = stock
  }

  private isInStock(): boolean {
    return this._stock > 0
  }

  get stock(): number {
    return this._stock
  }

  set stock(value: number) {
    if (value < 0) {
      throw new Error('Stock cannot be negative.')
    }
    this._stock = value
  }

  static calculateTax(price: number): number {
    return price * this.taxRate
  }

  getDetails(): string {
    return `ID: ${this.id}, Name: ${this.name}, Price: $${this.price}, Category: ${this.category}, Stock: ${this._stock}, Tax: $${Product.calculateTax(this.price)}`
  }
}

// 4. 创建一个子类 ElectronicProduct
class ElectronicProduct extends Product {
  warranty: number

  constructor(
    id: number,
    name: string,
    price: number,
    category: Category,
    stock: number,
    warranty: number
  ) {
    super(id, name, price, category, stock)
    this.warranty = warranty
  }

  getDetails(): string {
    const baseDetails = super.getDetails()
    return `${baseDetails}, Warranty: ${this.warranty} years`
  }
}

// 5. 使用泛型接口 Discountable<T>
interface Discountable<T> {
  applyDiscount(discount: T): void
}

// 实现 Discountable 接口的示例
class DiscountableProduct extends Product implements Discountable<number | string> {
  applyDiscount(discount: number | string): void {
    if (typeof discount === 'number') {
      this.price -= this.price * discount
    } else {
      this.price -= parseInt(discount, 10)
    }
  }
}

// 6. 定义一个函数类型 PromotionFunction
type PromotionFunction = (product: Product) => number

// 使用示例
const product = new Product(1, 'T-Shirt', 20, Category.Clothing, 10)
console.log(product.getDetails())

const electronicProduct = new ElectronicProduct(2, 'Laptop', 1000, Category.Electronics, 5, 2)
console.log(electronicProduct.getDetails())

const discountableProduct = new DiscountableProduct(3, 'Smartphone', 500, Category.Electronics, 8)
discountableProduct.applyDiscount(0.1) // 10% discount
console.log(discountableProduct.getDetails())
 
const promotionFunction: PromotionFunction = (product: Product) => {
  return product.price - Product.calculateTax(product.price)
}
console.log(`Promoted Price: $${promotionFunction(product)}`)
